Selenium Waits

Definition:
Selenium waits are used to establish synchronization between the script execution speed and the application’s response/rendering speed. Without waits, tests may fail due to elements not being ready (not loaded, visible, or interactable).

Types of Waits in Selenium
There are two main types of waits in Selenium WebDriver:

1. Implicit Waits
Definition: Tells WebDriver to wait for a certain amount of time when trying to find an element before throwing NoSuchElementException.

Nature: Global wait → applies to all findElement/findElements calls for the lifetime of the driver instance.

Drawback: Not flexible. Cannot be applied for specific conditions.

Methods:

pageLoadTimeout:
Sets the time to wait for a page load to complete before throwing TimeoutException.
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(30));

implicitlyWait:
Waits for elements to appear in DOM (not necessarily visible).
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));

setScriptTimeout:
Maximum time to wait for an asynchronous script (via executeAsyncScript).
driver.manage().timeouts().scriptTimeout(Duration.ofSeconds(20));

Note: From Selenium 4+, manage().timeouts() uses Duration instead of integers.
===========================================================
2. Explicit Waits

Definition: Explicit waits are conditional waits that are applied only for specific elements or conditions.

Nature: Local wait → applied only where defined.
Benefit: Dynamic – exits immediately once condition is met, saving time.

Examples of Explicit Waits:

(a) WebDriverWait (Preferred in most cases)

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("username")));


(b) FluentWait
Extension of WebDriverWait with more fine-grained control (polling frequency, ignored exceptions).

Wait<WebDriver> wait = new FluentWait<WebDriver>(oBrowser)
		.withTimeout(Duration.ofSeconds(40))
		.pollingEvery(Duration.ofSeconds(10))
		.ignoring(NoSuchElementException.class);

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
wait.until(ExpectedConditions.textToBePresentInElementLocated(By.xpath(""), "<text>"));

Common ExpectedConditions Used with Explicit Waits

elementToBeClickable(By locator)
visibilityOfElementLocated(By locator)
presenceOfElementLocated(By locator)
textToBePresentInElementLocated(By locator, "text")
alertIsPresent()
titleContains("title")
frameToBeAvailableAndSwitchToIt(By locator)
===========================================================
3. Static Waits
(a) Thread.sleep(ms)
Not a Selenium wait; it is a Java hard wait (static waits).
Always pauses execution for the given time (bad practice in automation).

=============================================================
Key Differences: Implicit Wait & Explicit Wait:
Implicit Wait	
Global for driver	
Simple to use	
Cannot wait for conditions	
May cause test delays if misused	

Explicit Wait
Applied only to specific element/condition
More flexible & powerful
Can wait for conditions like clickable, visible, text present
More efficient, recommended for dynamic apps


Best Practices

Prefer Explicit Waits over Implicit Waits for dynamic web apps.
Avoid mixing Implicit and Explicit Waits – it may lead to unpredictable behavior.
Avoid Thread.sleep() except for debugging or very rare cases.
Use FluentWait when custom polling and exception handling is needed.